#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int count = 0;
        int tail = nums.size() - 1;
        while (tail >= 2)
        {
            int right = tail - 1;
            int left = 0;
            while (left < right)
            {
                if (nums[left] + nums[right] <= nums[tail])
                {
                    left++;
                }
                else
                {
                    count += right - left;
                    right--;
                }
            }

            tail--;
        }

        return count;
    }
};